package cn.commander.model.response;

import cn.commander.model.CusPageInfo;
import cn.commander.utils.SpringPropertiesUtils;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;


public class ResponseData {

	private static final Logger LOGGER = LoggerFactory.getLogger(ResponseData.class);

	private String status;
	private String message;
	private ResponseObject data;

	public ResponseData() {
	}

	public ResponseData buildResponseData(String status) {
		return this.buildResponseData(status, null, null, null);
	}

	public ResponseData buildResponseData(String status, String message) {
		return this.buildResponseData(status, message, null, null);
	}

	public ResponseData buildResponseData(String status, Object object) {
		return this.buildResponseData(status, null, object, null);
	}

	public ResponseData buildResponseData(String status, String message, Object object) {
		return this.buildResponseData(status, message, object, null);
	}

	public ResponseData buildResponseData(String status, String message, Object object, CusPageInfo cusPageInfo) {
		this.status = status;
		if (StringUtils.isEmpty(message)) {
			this.message = SpringPropertiesUtils.parseStr(status);
		} else {
			this.message = message;
		}
		this.data = new ResponseObject();
		this.data.setObject(object);
		this.data.setCusPageInfo(cusPageInfo);
		Gson gson = new Gson();
		LOGGER.debug("\r\n接口返回信息：{}", gson.toJson(this));
		return this;
	}

	public String getStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}

	public ResponseObject getData() {
		return data;
	}

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", this.getStatus());
		map.put("message", this.getMessage());
		map.put("data", this.data);
		return map;
	}

	public String toJson() {
		return new Gson().toJson(this);
	}
}
